import { defineStore } from 'pinia';
import { Session } from '/@/utils/storage';
import { getUserInfo, login, refreshTokenApi } from '/@/api/login/index';
import { uavLogin, uavRefreshToken } from '../api/uav-open';

/**
 * @function useUserInfo
 * @returns {UserInfosStore}
 */
export const useUserInfo = defineStore('userInfo', {
	state: (): UserInfosState => ({
		userInfos: {
			userName: '',
			photo: '',
			time: 0,
			roles: [],
			authBtnList: [],
		},
	}),

	actions: {
		/**
		 * 登录方法
		 * @function login
		 * @async
		 * @param {Object} data - 登录数据
		 * @returns {Promise<Object>}
		 */
		async login(data: any, appCode?: string) {
			let userInfo = JSON.parse(JSON.stringify(data));
			userInfo.grant_type = 'password';
			userInfo.scope = 'server';
			if (userInfo.username == 'super') {
				userInfo.username = '0,super';
				Session.set('is_super_login', 1)
			} else {
				userInfo.username = `${appCode},${userInfo.username}`;
				Session.remove('is_super_login',)
			}
			return new Promise((resolve, reject) => {
				login(userInfo)
					.then((res) => {
						// 存储token 信息
						Session.set('token', res.access_token);
						Session.set('refresh_token', res.refresh_token);
						resolve(res);
					})
					.catch((err) => {
						// useMessage().error(err?.msg || '系统异常请联系管理员');
						reject(err);
					});
			});
		},

		/**
		 * 刷新token方法
		 * @function refreshToken
		 * @async
		 * @returns {Promise<any>}
		 */
		async refreshToken() {
			return new Promise((resolve, reject) => {
				const refreshToken = Session.get('refresh_token');
				refreshTokenApi(refreshToken)
					.then((res) => {
						// 存储token 信息
						Session.set('token', res.access_token);
						Session.set('refresh_token', res.refresh_token);
						resolve(res);
					})
					.catch((err) => {
						// useMessage().error(err.msg);
						reject(err);
					});
			});
		},
		/**
		 * 微信小程序 刷新token方法
		 * @returns
		 */
		async refreshTokenForWx() {
			return new Promise((resolve, reject) => {
				const refreshToken = Session.get('refresh_token');
				refreshTokenApiForWx(refreshToken)
					.then((res) => {
						// 存储token 信息
						Session.set('token', res.access_token);
						Session.set('refresh_token', res.refresh_token);
						resolve(res);
					})
					.catch((err) => {
						reject(err);
					});
			});
		},
		/**
		 * 无人机影像云用户登录
		 * @param param0 
		 * @returns 
		 */
		UavLoginByUsername() {
			return new Promise((resolve, reject) => {
				uavLogin().then((res) => {
					Session.set('uav_access_token', res.data.accessToken)
					Session.set('uav_refresh_token', res.data.refreshToken)
					Session.set('uav_user_compant_appkey', res.data.data)
					resolve(res);
				}).catch((error) => {
					reject(error);
				})
			})
		},
		// 无人机影像云刷新token
		UavRefreshToken() {
			return new Promise((resolve, reject) => {
				uavRefreshToken()
					.then((res) => {
						Session.set('uav_access_token', res.data.accessToken)
						Session.set('uav_refresh_token', res.data.refreshToken)
						resolve(res);
					})
					.catch((error) => {
						reject(error);
					});
			});
		},

		/**
		 * 获取用户信息方法
		 * @function setUserInfos
		 * @async
		 */
		async setUserInfos() {
			await getUserInfo().then((res) => {
				const userInfo: any = {
					user: res.data.sysUser,
					time: new Date().getTime(),
					roles: res.data.roles,
					authBtnList: res.data.permissions,
				};
				this.userInfos = userInfo;
				Session.set('regionCode', res.data.companyRegionCode);
			});
		},
		/**
		 * 清理用户信息
		 */
		clearUserInfos() {
			this.userInfos = {
				user: '',
				damei: '',
				time: 0,
				roles: [],
				authBtnList: [],
			};
		}
	},
});
